import { computed, ref } from 'vue';
import { updateEntity, deleteEntity } from '@/api/dashboard/dict';
import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
import { Modal, Message } from '@arco-design/web-vue';
import { validateForm } from '@/utils';

export let modalType = ref(0);
export let currRecord = ref({} as any);
let currSelect: any;

export function addNew() {
  modalType.value = 3;
  currRecord.value = {
    enabled: true,
    dictionaryLevel: 1,
  };
  validateForm(null, true);
}

export function addNext() {
  if (!currSelect) {
    Message.warning('请先选择一行');
    return;
  }
  modalType.value = 3;
  currRecord.value = {
    applicationId: currSelect.applicationId,
    parentId: currSelect.dictionaryId,
    parentName: currSelect.dictionaryName,
    dictionaryLevel: currSelect.dictionaryLevel + 1,
    enabled: true,
  };
  validateForm(null, true);
}

export function rowSelect(rowKeys: Array<string>, key: string, record: any) {
  currSelect = record;
}

export function btnView(record: any) {
  modalType.value = 1;
  currRecord.value = record;
}

export function btnEdit(record: any) {
  modalType.value = 2;
  currRecord.value = { ...record };
  validateForm(null, true);
}

export function btnDelete(record: any, call: Function) {
  Modal.confirm({
    title: '确认',
    content: `确定要删除【${record.dictionaryName}】吗？`,
    onOk: async function () {
      await deleteEntity(record.dictionaryId);
      call();
    },
  });
}

export async function btnEditSave(call: Function) {
  if (!validateForm()) {
    return;
  }
  modalType.value = 0;
  await updateEntity(currRecord.value);
  call();
}
